package pe.edu.upc.dew.pharma.util;

import java.io.Serializable;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.metaparadigm.jsonrpc.JSONRPCBridge;

public class ListenerJSON implements HttpSessionListener, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7530794843146437748L;
	private static final Log logger = LogFactory.getLog(ListenerJSON.class);
	
	public ListenerJSON() {
		if(logger.isInfoEnabled()){
			logger.info("ListenerJSON");
		}
	}

	public void sessionCreated(HttpSessionEvent sessionEvent) {
		if(logger.isInfoEnabled()){
			logger.info("ListenerJSON.sessionCreated");
		}
		HttpSession session = sessionEvent.getSession();
		JSONRPCBridge jsonBridge = null;
		jsonBridge = (JSONRPCBridge) session.getAttribute("JSONRPCBridge");
		if (jsonBridge == null) {
			jsonBridge = new JSONRPCBridge();
			session.setAttribute("JSONRPCBridge", jsonBridge);
		}
		ServletJSON servletJSON = new ServletJSON(session);
		jsonBridge.registerObject("servletJSON", servletJSON);
	}

	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
		
	}

}
